#include "Magie01.h"

// Animation de la magie01
Magie01::Magie01()
	:  magie01(nullptr)
	, isPlaying(false)
	, linkBoss(nullptr)
{
}

Magie01::Magie01(Boss* theBoss, int bossPositionX, int bossPositionY, int characterPositionX, int characterPositionY)
	: magieDebut("Assets/Artworks/magie_begin.png", 10, 15, false, 1, 1, 44, 44, characterPositionX, characterPositionY, 2.625f, false)
	, magie01(nullptr)
	, size(2.625f)
	, nbFrame(19)
	, frameRate(10)
	, currentTime(0)
	, currentFrame(0)
	, isPlaying(false)
	, etat(0)
	, linkBoss(theBoss)
{
	magie01 = SDLResources::GetInstance()->GetTexture("Assets\\Artworks\\magie01.png");

	bossPosition.x = bossPositionX;
	bossPosition.y = bossPositionY;
	characterPosition.x = characterPositionX;
	characterPosition.y = characterPositionY;

	magieDebut.SetVisible(false);

	//magieDebut.Play();

	// image_1
	SDL_Rect* rect = new SDL_Rect();
	rect->x = 0;
	rect->y = 0;
	rect->w = 6;
	rect->h = 16;
	listeSrcRect.push_back(rect);
	// image_2
	rect = new SDL_Rect();
	rect->x = 7;
	rect->y = 0;
	rect->w = 20 - rect->x;
	rect->h = 52 - rect->y;
	listeSrcRect.push_back(rect);
	// image_3
	rect = new SDL_Rect();
	rect->x = 22;
	rect->y = 0;
	rect->w = 46 - rect->x;
	rect->h = 132 - rect->y;
	listeSrcRect.push_back(rect);
	// image_4
	rect = new SDL_Rect();
	rect->x = 48;
	rect->y = 0;
	rect->w = 80 - rect->x;
	rect->h = 132 - rect->y;
	listeSrcRect.push_back(rect);
	// image_5
	rect = new SDL_Rect();
	rect->x = 82;
	rect->y = 0;
	rect->w = 124 - rect->x;
	rect->h = 132 - rect->y;
	listeSrcRect.push_back(rect);
	// image_6
	rect = new SDL_Rect();
	rect->x = 126;
	rect->y = 0;
	rect->w = 149 - rect->x;
	rect->h = 100 - rect->y;
	listeSrcRect.push_back(rect);
	// image_7
	rect = new SDL_Rect();
	rect->x = 151;
	rect->y = 0;
	rect->w = 174 - rect->x;
	rect->h = 132 - rect->y;
	listeSrcRect.push_back(rect);
	// image_8
	rect = new SDL_Rect();
	rect->x = 176;
	rect->y = 0;
	rect->w = 200 - rect->x;
	rect->h = 132 - rect->y;
	listeSrcRect.push_back(rect);
	// image_9
	rect = new SDL_Rect();
	rect->x = 208;
	rect->y = 0;
	rect->w = 272 - rect->x;
	rect->h = 64;
	listeSrcRect.push_back(rect);
	// image_10
	rect = new SDL_Rect();
	rect->x = 202;
	rect->y = 91;
	rect->w = 254 - rect->x;
	rect->h = 122 - rect->y;
	listeSrcRect.push_back(rect);

	for(std::vector<SDL_Rect*>::iterator iter = listeSrcRect.begin(); iter != listeSrcRect.end(); iter++)
	{
		rect = new SDL_Rect();
		rect->x = 0;
		rect->y = 0;
		rect->w = (*iter)->w * size;
		rect->h = (*iter)->h * size;
		/*rect->x = (*iter)->x;
		rect->y = (*iter)->y;
		rect->w = (*iter)->w;
		rect->h = (*iter)->h;*/
		listeDstRect.push_back(rect);
	}
}


Magie01::~Magie01()
{
	for(std::vector<SDL_Rect*>::iterator iter = listeSrcRect.begin(); iter != listeSrcRect.end(); iter++)
	{
		delete (*iter);
	}

	for(std::vector<SDL_Rect*>::iterator iter = listeDstRect.begin(); iter != listeDstRect.end(); iter++)
	{
		delete (*iter);
	}
}

void Magie01::PlayMagie()
{
	magieDebut.SetVisible(true);
	magieDebut.Play();
	isPlaying = true;
	etat = 0;
	currentTime = 0;
	currentFrame = 0;
}

void Magie01::ApplySurfaceMagie(int index, SDL_Texture* texture, SDL_Renderer* renderer)
{
	SDL_RenderCopy(renderer, texture, listeSrcRect[index], listeDstRect[index]);
}

void Magie01::Draw()
{
	if(isPlaying)
	{
		currentTime += SDLTimer::GetInstance()->GetDeltaTime();

		if(etat == 0)
		{
			if(currentTime >= 1.0f)
			{
				etat = 1;
			}
		}
		else if(etat == 1)
		{
			if(currentTime >= 1.0f/frameRate)
			{
				currentFrame++;
			
				if(currentFrame > nbFrame)
				{
					isPlaying = false;
					//currentFrame = 1;
				}

				currentTime = 0;
			}
				if(currentFrame == 1)
				{
					listeDstRect[0]->x = bossPosition.x;
					listeDstRect[0]->y = 1;
					ApplySurfaceMagie(0, magie01, SDLRenderer::GetInstance()->GetRenderer());
				}
				else if(currentFrame == 2)
				{
					listeDstRect[1]->x = bossPosition.x;
					listeDstRect[1]->y = 0;
					ApplySurfaceMagie(1, magie01, SDLRenderer::GetInstance()->GetRenderer());
				}
				else if(currentFrame == 3)
				{
					listeDstRect[2]->x = bossPosition.x;
					listeDstRect[2]->y = 0;
					ApplySurfaceMagie(2, magie01, SDLRenderer::GetInstance()->GetRenderer());

					linkBoss->SetShaking(true);
				}
				else if(currentFrame == 4)
				{
					listeDstRect[3]->x = bossPosition.x;
					listeDstRect[3]->y = 0;
					ApplySurfaceMagie(3, magie01, SDLRenderer::GetInstance()->GetRenderer());
				}
				else if(currentFrame == 5)
				{
					listeDstRect[4]->x = bossPosition.x;
					listeDstRect[4]->y = 0;
					ApplySurfaceMagie(4, magie01, SDLRenderer::GetInstance()->GetRenderer());
				}
				else if(currentFrame == 6)
				{
					listeDstRect[1]->x = bossPosition.x;
					listeDstRect[1]->y = 0;
					ApplySurfaceMagie(1, magie01, SDLRenderer::GetInstance()->GetRenderer());
				}
				else if(currentFrame == 7)
				{
					listeDstRect[5]->x = bossPosition.x;
					listeDstRect[5]->y = 0;
					ApplySurfaceMagie(5, magie01, SDLRenderer::GetInstance()->GetRenderer());
				}
				else if(currentFrame == 8)
				{
					listeDstRect[2]->x = bossPosition.x;
					listeDstRect[2]->y = 0;
					ApplySurfaceMagie(2, magie01, SDLRenderer::GetInstance()->GetRenderer());
				}
				else if(currentFrame == 9)
				{
					listeDstRect[6]->x = bossPosition.x;
					listeDstRect[6]->y = 0;
					ApplySurfaceMagie(6, magie01, SDLRenderer::GetInstance()->GetRenderer());
				}

				else if(currentFrame == 10)
				{
					listeDstRect[0]->x = bossPosition.x - 10;
					listeDstRect[0]->y = 0;
					ApplySurfaceMagie(0, magie01, SDLRenderer::GetInstance()->GetRenderer());

					listeDstRect[2]->x = bossPosition.x + 10;
					listeDstRect[2]->y = 0;
					ApplySurfaceMagie(2, magie01, SDLRenderer::GetInstance()->GetRenderer());
				}
				else if(currentFrame == 11)
				{
					listeDstRect[1]->x = bossPosition.x - 15;
					listeDstRect[1]->y = 0;
					ApplySurfaceMagie(1, magie01, SDLRenderer::GetInstance()->GetRenderer());

					listeDstRect[7]->x = bossPosition.x + 15;
					listeDstRect[7]->y = 0;
					ApplySurfaceMagie(7, magie01, SDLRenderer::GetInstance()->GetRenderer());
				}
				else if(currentFrame == 12)
				{
					listeDstRect[5]->x = bossPosition.x - 35;
					listeDstRect[5]->y = 0;
					ApplySurfaceMagie(5, magie01, SDLRenderer::GetInstance()->GetRenderer());

					listeDstRect[3]->x = bossPosition.x + 35;
					listeDstRect[3]->y = 0;
					ApplySurfaceMagie(3, magie01, SDLRenderer::GetInstance()->GetRenderer());
				}
				else if(currentFrame == 13)
				{
					listeDstRect[2]->x = bossPosition.x - 35;
					listeDstRect[2]->y = 0;
					ApplySurfaceMagie(2, magie01, SDLRenderer::GetInstance()->GetRenderer());

					listeDstRect[6]->x = bossPosition.x + 35;
					listeDstRect[6]->y = 0;
					ApplySurfaceMagie(6, magie01, SDLRenderer::GetInstance()->GetRenderer());
				}
				else if(currentFrame == 14)
				{
					listeDstRect[6]->x = bossPosition.x - 35;
					listeDstRect[6]->y = 0;
					ApplySurfaceMagie(6, magie01, SDLRenderer::GetInstance()->GetRenderer());

					listeDstRect[4]->x = bossPosition.x + 35;
					listeDstRect[4]->y = 0;
					ApplySurfaceMagie(4, magie01, SDLRenderer::GetInstance()->GetRenderer());
				}

				else if(currentFrame == 15)
				{
					listeDstRect[2]->x = bossPosition.x;
					listeDstRect[2]->y = 0;
					ApplySurfaceMagie(2, magie01, SDLRenderer::GetInstance()->GetRenderer());
				}
				else if(currentFrame == 16)
				{
					listeDstRect[7]->x = bossPosition.x;
					listeDstRect[7]->y = 0;
					ApplySurfaceMagie(7, magie01, SDLRenderer::GetInstance()->GetRenderer());
				}
				else if(currentFrame == 17)
				{
					listeDstRect[3]->x = bossPosition.x;
					listeDstRect[3]->y = 0;
					ApplySurfaceMagie(3, magie01, SDLRenderer::GetInstance()->GetRenderer());
				}
				else if(currentFrame == 18)
				{
					listeDstRect[4]->x = bossPosition.x;
					listeDstRect[4]->y = 0;
					ApplySurfaceMagie(4, magie01, SDLRenderer::GetInstance()->GetRenderer());
				}
				else if(currentFrame == 19)
				{
					listeDstRect[2]->x = bossPosition.x;
					listeDstRect[2]->y = 0;
					ApplySurfaceMagie(2, magie01, SDLRenderer::GetInstance()->GetRenderer());
				}

			
		}
	}
}